package com.ruoyi.mdm.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mdm.domain.SkuMaster;
import com.ruoyi.mdm.domain.SpuInfoEntity;
import com.ruoyi.mdm.mapper.SkuMasterMapper;
import com.ruoyi.mdm.service.ISkuMasterService;
import com.ruoyi.mdm.service.ISpuInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.mdm.mapper.SkuProductMasterMapper;
import com.ruoyi.mdm.domain.SkuProductMaster;
import com.ruoyi.mdm.service.ISkuProductMasterService;
import org.springframework.transaction.annotation.Transactional;

/**
 * spu主Service业务层处理
 *
 * @author shiseido
 * @date 2025-03-01
 */
@Service
public class SkuProductMasterServiceImpl implements ISkuProductMasterService {
    @Autowired
    private SkuProductMasterMapper skuProductMasterMapper;

    @Autowired
    private ISpuInfoService spuInfoService;

    @Autowired
    private SkuMasterMapper skuMasterMapper;

    @Autowired
    private ISkuMasterService skuMasterService;

    /**
     * 查询spu主
     *
     * @param id spu主主键
     * @return spu主
     */
    @Override
    public SkuProductMaster selectSkuProductMasterById(Long id) {
        return skuProductMasterMapper.selectSkuProductMasterById(id);
    }

    /**
     * 查询spu主列表
     *
     * @param skuProductMaster spu主
     * @return spu主
     */
    @Override
    public List<SkuProductMaster> selectSkuProductMasterList(SkuProductMaster skuProductMaster) {
        return skuProductMasterMapper.selectSkuProductMasterList(skuProductMaster);
    }

    /**
     * 新增spu主
     *
     * @param skuProductMaster spu主
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSkuProductMaster(SkuProductMaster skuProductMaster) {

        // 相同品牌，SPU中文名称不能相同
        SkuProductMaster skuProductMasterCheck = new SkuProductMaster();
        skuProductMasterCheck.setOwnerId(skuProductMaster.getOwnerId());
        skuProductMasterCheck.setSpuName(skuProductMaster.getSpuName());
        List<SkuProductMaster> skuProductMasterCheckList
                = skuProductMasterMapper.selectSkuProductMasterList(skuProductMasterCheck);
        if (StringUtils.isNotEmpty(skuProductMasterCheckList)) {
            throw new ServiceException("同一个品牌下，已存在此SPU！");
        }

        skuProductMaster.setCreateTime(DateUtils.getNowDate());
        skuProductMaster.setUpdateTime(DateUtils.getNowDate());
        skuProductMasterMapper.insertSkuProductMaster(skuProductMaster);

        // 生成编码
        skuProductMaster.setSpuCode("P" + skuProductMaster.getOwnerId() + String.format("%07d", skuProductMaster.getId()));
        skuProductMasterMapper.updateSkuProductMaster(skuProductMaster);

        // 同步ES 异步处理
        syncSpuToEs(skuProductMaster.getId());

        return 1;
    }

    /**
     * 修改spu主
     *
     * @param skuProductMaster spu主
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSkuProductMaster(SkuProductMaster skuProductMaster) {

        // 相同品牌，SPU中文名称不能相同
        SkuProductMaster skuProductMasterCheck = new SkuProductMaster();
        skuProductMasterCheck.setOwnerId(skuProductMaster.getOwnerId());
        skuProductMasterCheck.setSpuName(skuProductMaster.getSpuName());
        List<SkuProductMaster> skuProductMasterCheckList
                = skuProductMasterMapper.selectSkuProductMasterList(skuProductMasterCheck);
        if (StringUtils.isNotEmpty(skuProductMasterCheckList)) {
            skuProductMasterCheck = skuProductMasterCheckList.get(0);
            if (skuProductMasterCheck.getId().compareTo(skuProductMaster.getId()) != 0) {
                throw new ServiceException("同一个品牌下，已存在此SPU！");
            }
        }

        skuProductMaster.setUpdateTime(DateUtils.getNowDate());
        skuProductMasterMapper.updateSkuProductMaster(skuProductMaster);

        // 同步ES 异步处理
        syncSpuToEs(skuProductMaster.getId());

        return 1;
    }

    @Async
    public void syncSpuToEs(Long spuId) {

        // 同步SPUES
        SkuProductMaster skuProductMaster = skuProductMasterMapper.selectSkuProductMasterById(spuId);
        syncSpuToEs(skuProductMaster);

        // 同步新增ES-SKU
        SkuMaster skuSelectMaster = new SkuMaster();
        skuSelectMaster.setSpuId(spuId);
        List<SkuMaster> skuMasterList = skuMasterMapper.selectSkuMasterToEs(skuSelectMaster);
        if(!skuMasterList.isEmpty()){
            skuMasterService.syncSkuToEs(skuMasterList, false);
        }

    }

    /**
     * 批量删除spu主
     *
     * @param ids 需要删除的spu主主键
     * @return 结果
     */
    @Override
    public int deleteSkuProductMasterByIds(Long[] ids) {
        return skuProductMasterMapper.deleteSkuProductMasterByIds(ids);
    }

    /**
     * 删除spu主信息
     *
     * @param id spu主主键
     * @return 结果
     */
    @Override
    public int deleteSkuProductMasterById(Long id) {
        return skuProductMasterMapper.deleteSkuProductMasterById(id);
    }

    /**
     * 同步SPU到ES
     *
     * @param skuProductMaster spu信息
     */
    @Override
    public void syncSpuToEs(SkuProductMaster skuProductMaster) {

        // 同步更新ES-SPU
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(skuProductMaster, spuInfoEntity);
        spuInfoEntity.setCreateTime(DateUtils.parseDateToTStr(skuProductMaster.getCreateTime()));
        spuInfoEntity.setUpdateTime(DateUtils.parseDateToTStr(skuProductMaster.getUpdateTime()));

        // 品牌名称
//        String goodsBrandName = DictUtils.getDictLabel("goods_brand", skuProductMaster.getOwnerId());
        StringBuffer keyword = new StringBuffer();
//        keyword = keyword.append(skuProductMaster.getOwnerId()).append(" ");
//        keyword = keyword.append(goodsBrandName).append(" ");
        keyword = keyword.append(skuProductMaster.getSpuName()).append(" ");
        keyword = keyword.append(StringUtils.nvl(skuProductMaster.getSpuNameEn(), "")).append(" ");
        keyword = keyword.append(StringUtils.nvl(skuProductMaster.getSpuShortName(), "")).append(" ");
        keyword = keyword.append(StringUtils.nvl(skuProductMaster.getSpuNickName(), ""));
        spuInfoEntity.setKeyword(keyword.toString());

        spuInfoService.add(spuInfoEntity);
    }

    /**
     * 同步SPU到ES
     *
     * @param skuProductMasterList spu信息
     */
    @Override
    public void syncSpuToEs(List<SkuProductMaster> skuProductMasterList) {

        List<SpuInfoEntity> spuInfoEntityList = new ArrayList<>();

        for (SkuProductMaster skuProductMaster : skuProductMasterList) {

            // 同步更新ES-SPU
            SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
            BeanUtils.copyProperties(skuProductMaster, spuInfoEntity);
            spuInfoEntity.setCreateTime(DateUtils.parseDateToTStr(skuProductMaster.getCreateTime()));
            spuInfoEntity.setUpdateTime(DateUtils.parseDateToTStr(skuProductMaster.getUpdateTime()));

            // 品牌名称
//            String goodsBrandName = DictUtils.getDictLabel("goods_brand", skuProductMaster.getOwnerId());
            StringBuffer keyword = new StringBuffer();
//            keyword = keyword.append(skuProductMaster.getOwnerId()).append(" ");
//            keyword = keyword.append(goodsBrandName).append(" ");
            keyword = keyword.append(skuProductMaster.getSpuName()).append(" ");
            keyword = keyword.append(StringUtils.nvl(skuProductMaster.getSpuNameEn(), "")).append(" ");
            keyword = keyword.append(StringUtils.nvl(skuProductMaster.getSpuShortName(),"")).append(" ");
            keyword = keyword.append(StringUtils.nvl(skuProductMaster.getSpuNickName(), ""));
            spuInfoEntity.setKeyword(keyword.toString());

            spuInfoEntityList.add(spuInfoEntity);
        }

        spuInfoService.deleteAll();
        spuInfoService.addAll(spuInfoEntityList);
    }
}
